package com.ulife.order.components.core.refund.factory;

import com.google.common.base.Strings;
import com.ulife.base.entities.order.SaleOrderPay;
import com.ulife.base.entities.order.SoRefundBill;
import com.ulife.base.logics.order.SaleOrderPayLogic;
import com.ulife.order.api.core.refund.RefundBillStatus;
import com.ulife.order.api.core.refund.RefundStatus;
import com.ulife.order.components.core.result.BaseResult;
import com.ulife.order.components.core.result.ResultCode;
import com.ulife.order.service.enumeration.PayStatus;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DecimalFormat;

/**
 * Created by Lbq on 2016/05/26.
 */
@Component("defaultRefundFactory")
public class DefaultRefundFactory implements IRefundFactory {

    @Resource
    SaleOrderPayLogic saleOrderPayLogic;

    @Override
    public BaseResult processRefund(SoRefundBill soRefundBill,long operator){
        BaseResult result = new BaseResult();
        SaleOrderPay saleOrderPay = saleOrderPayLogic.get(soRefundBill.getSoPayId());
        if (saleOrderPay == null || saleOrderPay.getStatus() != PayStatus.Success.value()) {
            result.setMessage("支付记录不存在或支付状态不正确");
            return result;
        }
        if(soRefundBill.getStatus() == RefundBillStatus.Finished.value()
                || soRefundBill.getRefundStatus() == RefundStatus.Succeed.value()){
            result.setMessage("退款单据已经完成退款操作");
            result.setCode(ResultCode.Repeat);
            return result;
        }
        if(Strings.isNullOrEmpty(soRefundBill.getRefundChannel())){
            result.setMessage("退款渠道不能为空");
            result.setCode(ResultCode.Failure);
            return result;
        }
        if(Strings.isNullOrEmpty(soRefundBill.getRefundBankSerialNumber())){
            result.setMessage("退款银行流水号不能为空");
            result.setCode(ResultCode.Failure);
            return result;
        }
        result.setCode(ResultCode.Success);
        result.setData(RefundStatus.Succeed);
        //您的订单退款X元已退款到您的余额/微信/支付宝（注：这里显示实际退款方式）账号，请注意查收。如有问题请致电400-687-909
        result.setMessage("您的订单退款"
                .concat(new DecimalFormat("####0.00").format(soRefundBill.getRefundAmount()))
                .concat("元已退款到您的")
                .concat(saleOrderPay.getPayChannel())
                .concat("账号，请注意查收。如有问题请致电400-687-909"));
        return result;
    }
}
